package com.xxb.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import com.xxb.entity.Order;
import com.xxb.entity.OrderItem;
import com.xxb.util.DBHelp;
import com.xxb.util.Rowmapper;

public class OrderItemDao {
	
	private DBHelp<OrderItem> db = new DBHelp<OrderItem>();
		
	public int save(OrderItem oitem){
		String sql = "insert into t_orderitem(oid,pid,pnum,totalprice) values(?,?,?,?)";
		return db.executeSQL(sql, oitem.getOid(),oitem.getPid(),oitem.getPnum(),oitem.getTotalprice());
	}
	
	public List<OrderItem> findByuid(int uid){
		String sql = "SELECT state,pnum,totalprice,oid FROM t_order LEFT JOIN t_orderitem ON t_order.id = oid WHERE uid=?";
		return db.quertyList(sql, new Rowmapper<OrderItem>() {

			public OrderItem mapRow(ResultSet rs) throws SQLException {
				OrderItem item = new OrderItem();
				Order o = new Order();
				
				o.setState(rs.getString("state"));
				item.setPnum(rs.getInt("pnum"));
				item.setTotalprice(rs.getFloat("totalprice"));
				item.setOid(rs.getString("oid"));
				item.setOrder(o);
				return item;
			}
			
	
		},uid); 
	}
}
